# -*- coding: utf-8 -*-
#coding=utf-8

import config
from action.user.base import userAction
from model.album import album
'''
User controller: producing album user views.
'''
class refererAction(userAction):
    def __init__(self, name = '专辑管理'):
        userAction.__init__(self, name)

    def GET(self, name):
        if name == 'list':
            return self.list()
        elif name == 'delete':
            return self.delete()
        elif name == 'listForBatchDel':
            return self.listForBatchDel()
        elif name == 'add':
            return self.add()
        elif name == 'edit':
            return self.edit()
        else:
            return self.notFound()

    def POST(self, name):
        if name == 'save':
            return self.save()
        elif name == 'deleteBatch':
            return self.deleteBatch()  
        elif name == 'modify':
            return self.modify()
        else:
            return self.notFound()

    def list(self):
        inputParams = self.getInput()
        page = int(inputParams['page']) if inputParams.has_key('page') else 1
        count = config.COUNT_PER_PAGE
        offset= (page-1)*count if page > 0 else 0
   
        condition = {'owner':self.userid}
        albumObj = album()
        listData = albumObj.getOne('COUNT(*) AS `total`', condition)
        totalCount = listData['total']
        albumList = albumObj.getList('*', condition, '', str(offset) + ',' + str(count))
        pageString = self.getPageStr(self.makeUrl('/user/album/list'), page, count, totalCount)

        self.privData['ALBUM_LIST'] = albumList
        self.privData['PAGE_STRING'] = pageString

        return self.display('albumList')

    def listForBatchDel(self):
        inputParams = self.getInput()
        page = int(inputParams['page']) if inputParams.has_key('page') else 1
        count = config.COUNT_PER_PAGE
        offset= (page-1)*count if page > 0 else 0
   
        condition = {'owner':self.userid}
        albumObj = album()
        listData = albumObj.getOne('COUNT(*) AS `total`', condition)
        totalCount = listData['total']
        albumList = albumObj.getList('*', condition, '', str(offset) + ',' + str(count))
        pageString = self.getPageStr(self.makeUrl('/user/album/list'), page, count, totalCount)

        self.privData['ALBUM_LIST'] = albumList
        self.privData['PAGE_STRING'] = pageString

        return self.display('albumBatchDel')

    def delete(self):
        inputParams = self.getInput()

        try:
            condition = { 'id': int(inputParams['id']) }
            album().delete(condition)
        except Exception, e:
            return self.error(msg = '专辑删除失败: %s' % e, url=self.makeUrl('/user/album/list'))
    
        return self.success(msg='专辑删除成功', url=self.makeUrl('/user/album/list'))

    def deleteBatch(self):
        inputParams = self.getInput()
        idList = inputParams['delitems'].split(",")
        if (len(idList)==1):
            return self.error(msg='不能删除系统专辑', url=self.makeUrl('/user/album/list'))
        elif(len(idList)>1):
            idList.remove('1')
        try:
            condition = ' id IN (' + ','.join(idList) +')'
            album().delete(condition)
        except Exception, e:
            return self.error(msg='专辑删除失败: %s' % e, url=self.makeUrl('/user/album/list'))

        return self.success(msg='专辑删除成功', url=self.makeUrl('/user/album/list'))
        
    def add(self):
        return self.display('albumAdd')
    
    def modify(self):
        inputParams= self.getInput()

        try:
            data={
                'name':         inputParams['name'],
                'desc':         inputParams['desc'],
                'owner':        self.userid,
            }
            album().update(data, {'id': int(inputParams['id'])})
        except Exception, e:
            return self.error(msg = '专辑修改失败: %s' % e, url=self.makeUrl('/user/album/list'))

        return self.success('专辑修改成功', url=self.makeUrl('/user/album/list'))

    def edit(self):
        inputParams = self.getInput()

        albumObj = album().getOne('*', {'id': int(inputParams['id'])})
        if not albumObj:
            return self.error(msg = '专辑不存在', url=self.makeUrl('/user/album/list'))

        self.privData['ALBUM_ID'] =   albumObj['id']
        self.privData['ALBUM_NAME'] = albumObj['name']
        self.privData['ALBUM_DESC'] = albumObj['desc']
        return self.display('albumEdit')


    def save(self):
        userInput= self.getInput()
        try:
            data = {
                'name':         userInput['name'],
                'desc':         userInput['desc'],
                'owner':        self.userid,
            }
            album().insert(data)
        except Exception, e:
            return self.error(msg = '专辑保存失败: %s' % e, url=self.makeUrl('/user/album/list'))
        return self.success('专辑保存成功', url=self.makeUrl('/user/album/list'))
